#include"SLN.h"

void SLNPrint(SLNode* phead)
{
	SLNode* pcur = phead;
	while (pcur)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("NULL\n");
}

SLNode* SLNApply(SLNDataType x)
{
	SLNode* NewNode = (SLNode*)malloc(sizeof(SLNode));
	if (NewNode == NULL)
	{
		perror("malloc failed!");
		exit(1);
	}
	NewNode->data = x;
	NewNode->next = NULL;
	return NewNode;
}

void SLNPushBack(SLNode** pphead, SLNDataType x) 
{
	SLNode* NewNode = SLNApply(x);
	if (*pphead == NULL)
	{
		*pphead = NewNode;
	}
	else
	{
		SLNode* ptail = *pphead;
		while (ptail->next)
		{
			ptail = ptail->next;
		}
		ptail->next = NewNode;
	}
}

void SLNPushFront(SLNode** pphead, SLNDataType x)
{
	SLNode* NewNode = SLNApply(x);
	if (*pphead == NULL)
	{
		*pphead = NewNode;
	}
	else
	{
		NewNode->next = *pphead;
		*pphead = NewNode;
	}
}